import Phaser from "phaser";
import { MainScene } from "../scenes";
import { Polygon } from "./Polygon";

export class Oval extends Phaser.Physics.Arcade.Sprite {

    text: Phaser.GameObjects.Text
    food: Polygon | undefined
    health = 100
    row=0

    constructor(scene: MainScene, row: number, texture: string | Phaser.Textures.Texture) {

        super(scene, 960, scene.upBorder + row * 100 + 50, texture)
        scene.add.existing(this)
        scene.physics.add.existing(this)
        this.text = scene.add.text(this.x, this.y, 'run')
        this.text.setOrigin(0.5, 0.5)
        this.row=row
    }

    protected preUpdate(time: number, delta: number): void {
        super.preUpdate(time, delta)
        this.text.setPosition(this.x, this.y)
        if (this.body?.velocity.x === 0) {
            this.text.setText('eat')
            if (this.food !== undefined) {
                this.food.health--
            }
        } else {
            this.text.setText('run')
        }

        if (this.food && this.food.health <= 0) {
            this.food = undefined
            this.setVelocityX(-15)
        }
        if (this.x <= 0) {
            this.scene.scene.pause()
            this.scene.add.text(480, 290, 'game over')
        }
        // console.log('Attacker',this.body?.velocity.x)
    }
    destroy(fromScene?: boolean): void {
        this.text.destroy()
        super.destroy()
    }

}